import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/auth'
import store from '@/store/index'

import Home from '@/views/home/home'    // 首页
import Login from '@/views/login/Login.vue'     // 登录
import UserInfo from '@/views/userInfo/UserInfo'     // 个人信息
import Collect from '@/views/collect/collect.vue'  // 个人收藏
import Order from '@/views/order/index'  // 我的订单
import PaySuccess from '@/views/order/paySuccess'  //支付成功
import Detail from '@/views/Components/Container/Detail'  // 商品详情
import ShopCar from '@/views/shopCar/cart.vue'  // 购物车
import Settlement from '@/views/shopCar/ConfirmOrder.vue'  // 结算页面

/**
 * 个人信息中心子页面
 */
import Info from '@/views/userInfo/Components/Info.vue'
import ModifyPass from "@/views/userInfo/Components/ModifyPass.vue";
import UserAddress from "@/views/userInfo/Components/UserAddress.vue";

/**
 * 订单子页面
 */
import orderSuccess from "@/views/order/orderSuccess.vue";
Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/home',
    },
    {
        path: '/home',  //首页
        name: 'Home',
        component: Home,
        meta: { auth: false }
    },
    {
        path: '/userInfo',   // 个人信息
        name: 'UserInfo',
        redirect: "/info",
        component: UserInfo,
        meta: { auth: true },
        children: [
            {
                path: '/info',
                component: Info,
            },
            {
                path: '/modifyPass',
                component: ModifyPass,
            },
            {
                path: '/userAddress',
                component: UserAddress,
            }
        ]
    },
    {
        path: '/collect',   // 个人收藏
        name: 'Collect',
        component: Collect,
        meta: { auth: true }
    },
    {
        path: '/order',   // 我的订单
        name: 'Order',
        component: Order,
        meta: { auth: true }
    },
    {
        path: '/orderSuccess',   // 订单成功页面
        name: 'OrderSuccess',
        component: orderSuccess,
        meta: { auth: true }
    },
    {
        path: '/paySuccess',   // 支付成功页面
        name: 'PaySuccess',
        component: PaySuccess,
        meta: { auth: true }
    },
    {
        path: '/detail',   // 商品详情
        name: 'Detail',
        component: Detail,
        meta: { auth: true }
    },
    {
        path: '/shopCar',   // 购物车
        name: 'ShopCar',
        component: ShopCar,
        meta: { auth: true }
    },
    {
        path: '/settlement',   // 结算页面
        name: 'Settlement',
        component: Settlement,
        meta: { auth: true }
    },
    {
        path: '/login',   // 登录
        name: 'Login',
        component: Login,
        meta: { auth: false }

    },
    {
        path: '/404',
        component: () => import('@/views/404'),
        meta: { auth: false }
    },
    { path: '*', redirect: '/404', hidden: true }
]

const router = new VueRouter({
    scrollBehavior: () => ({ y: 0 }),
    routes
})

const whiteList = ['/login', '/home', '/shopCar']
const notHeadFootList = ['/login']   //判断是登录页和注册页时，不显示头部和底部
router.beforeEach(async (to, from, next) => {
    if (notHeadFootList.indexOf(to.path) !== -1) {
        await store.dispatch("changeShowHeadFoot", false)
    } else {
        await store.dispatch("changeShowHeadFoot", true)
    }
    const hasToken = getToken()
    if (hasToken) {

        if (to.path === '/login') {
            next({ path: '/home' })
        } else {
            const hasUserInfo = store.state.userInfo
            if (hasUserInfo) {
                next()
            } else {
                try {
                    await store.dispatch('getInfo')
                    next()
                } catch (error) {
                    await store.dispatch('resetToken')
                    next('/login')
                }
            }
        }
    } else {

        if (whiteList.indexOf(to.path) !== -1) {
            next()
        } else {
            next('/login')
        }
    }
})



export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}

export default router
